# -*- coding: utf-8 -*-

import json


class Side(object):
    WHITE = 'white'
    BLACK = 'black'


class Role(object):
    PARTICIPANT = 1
    OBSERVER = 2


class Code(object):
    SUCESS = 1
    ERROR = 2


class ChessMsg(object):

    def __init__(self, cli=None, grp=None, role=None, side=None, x=None, y=None, t=None, code=None, ext=None):
        self.cli = cli
        self.grp = grp
        self.role = role
        self.side = side
        self.x = x
        self.y = y
        self.t = t
        self.code = code
        self.ext = ext

    def to_json(self):
        _msg = dict()
        _msg['cli'] = self.cli
        _msg['grp'] = self.grp
        _msg['role'] = self.role
        _msg['side'] = self.side
        _msg['x'] = self.x
        _msg['y'] = self.y
        _msg['t'] = self.t
        _msg['code'] = self.code
        _msg['ext'] = self.ext
        return json.dumps(_msg)

    def __repr__(self):
        return self.to_json()

    __str__ = __repr__

    @staticmethod
    def json2obj(json_data):
        if not json_data:
            return None
        _dict_data = json.loads(json_data)
        return ChessMsg(cli=_dict_data.get('cli'),
                        grp=_dict_data.get('grp'),
                        role=_dict_data.get('role'),
                        side=_dict_data.get('side'),
                        x=_dict_data.get('x'),
                        y=_dict_data.get('y'),
                        t=_dict_data.get('t'),
                        code=_dict_data.get('code'),
                        ext=_dict_data.get('ext'))
